#!/usr/bin/python2.7
#
# Copyright 2012 Twist Inc.
#
#
import datetime
import time
import unittest

from google.appengine.api import logservice
from google.appengine.api.logservice import log_service_pb

from log2gs import log2csv
from tests.utils import TestCase


class Log2GsTest(TestCase):
    def test_log2csv(self):
        sample_log = log_service_pb.RequestLog()
        sample_log.set_start_time(1351634996 * 1000 * 1000)
        sample_log.set_user_agent('One Two Three/Mozilla ("CPU")')
        app_log = sample_log.add_line()
        app_log.set_time(time.time())
        app_log.set_level(logservice.LOG_LEVEL_DEBUG)
        app_log.set_log_message('Property: new_user=True')

        app_log = sample_log.add_line()
        app_log.set_time(time.time())
        app_log.set_level(logservice.LOG_LEVEL_WARNING)
        app_log.set_log_message('Property: new_user=123445')

        app_log = sample_log.add_line()
        app_log.set_time(time.time())
        app_log.set_level(logservice.LOG_LEVEL_WARNING)
        app_log.set_log_message('The world is: ok or burning')

        generator = log2csv(logservice.RequestLog(request_log=sample_log))
        first_line = generator.next()
        self.assertEqual('1351634996.0,,,0,0.0,0,False,,"One Two Three/Mozilla (""CPU"")",'
                         ',,,,,True,,,,'
                         '"Property: new_user=123445\nThe world is: ok or burning",,,'
                         '2012-10-30 14:09:56.000001\r\n',
                         first_line)
        with self.assertRaises(StopIteration):
            generator.next()
